//
//  MHScrollActionSheetView.swift
//  Kool Car
//
//  Created by huangweiqiang on 2022/4/23.
//

import UIKit
 
typealias MHScrollActionSheetViewBlock = (Any, NSInteger, NSInteger)-> Void
class MHScrollActionSheetView: UIViewController {
    var callBack: MHScrollActionSheetViewBlock!
    var secondArr:[[Any]] = []
    var bkgBtn: UIButton!
    
    var contentView: UIView! //装载tableView背景圆角时用
    var collectionView: UICollectionView!
    
    var headerView: UIView!
    var footerView: UIView!
    var selectItem: Any!
    var defaultHighLight: Bool = true //是否默认选中的高亮
    
    var titleLab: UILabel!
    var scrollIndex: NSInteger = 0
    var titleArr: [String] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        //背景蒙层
        self.view.backgroundColor = UIColor.clear
        bkgBtn = UIButton.init(type: UIButtonType.custom)
        bkgBtn.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight)
        bkgBtn.backgroundColor =  UIColor.init(R: 0, G: 0, B: 0, alpha: 0)
        bkgBtn.addTarget(self, action: #selector(hiddenActionView), for: .touchUpInside)
        self.view.addSubview(bkgBtn)
        
        ///
        self.contentView = UIView.init(frame: CGRect.init(x: 0, y: kScreenHeight, width: kScreenWidth, height: kScreenHeight))
        self.contentView.backgroundColor = UIColor.white
        self.bkgBtn.addSubview(self.contentView)
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize.init(width: kScreenWidth, height: 168)
        layout.scrollDirection = .horizontal
        collectionView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.register(MHScrollItemCell.self, forCellWithReuseIdentifier: "MHScrollItemCell")
        collectionView.backgroundColor = UIColor.white
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.isScrollEnabled = false
        self.contentView.addSubview(collectionView)
        
        self.headerView = self.setUpHeaderView()
        self.footerView = self.setUpFooterView()
        self.contentView.addSubview(self.headerView)
        self.contentView.addSubview(self.footerView)
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.startAnimationAction()
    }
    
    ///页面展示动画
    @objc func startAnimationAction(){
        let tempHH: CGFloat = 168 //CGFloat(self.secondArr.count * 56)
        self.collectionView.frame.size.height = tempHH
        
        let headerFooter = self.headerView.frame.size.height + self.footerView.frame.size.height
        self.contentView.frame.size.height = tempHH + headerFooter
        self.contentView.round(byRoundingCorners: UIRectCorner.init(arrayLiteral: .topLeft, .topRight), cornerRadi: 6)

        self.headerView.frame.origin = CGPoint.init(x: 0, y: 0)
        self.collectionView.frame.origin = CGPoint.init(x: 0, y: self.headerView.frame.maxY)
        self.footerView.frame.origin = CGPoint.init(x: 0, y: self.collectionView.frame.maxY)
        let opts = UIViewAnimationOptions.curveEaseInOut
        UIView.animate(withDuration: 0.24, delay: 0, options: opts, animations: {
            self.bkgBtn.backgroundColor =  UIColor.init(R: 0, G: 0, B: 0, alpha: 0.7)
            self.contentView.frame.origin.y = kScreenHeight - tempHH - headerFooter
        }, completion: { _ in
            if self.titleArr.count == 0 {
                //第一行做圆角处理
            }
        })
    }
    
    @objc func hiddenActionView(){
        let opts = UIViewAnimationOptions.curveEaseInOut
        UIView.animate(withDuration: 0.24, delay: 0, options: opts, animations: {
            self.bkgBtn.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0)
            self.contentView.frame.origin.y = kScreenHeight
        }, completion: { _ in
            self.dismiss(animated: false, completion: nil)
        })
    }
    
    func setUpHeaderView() -> UIView {
        let tempH: CGFloat = self.titleArr.count == 0 ? 0 : 56
        let tempHeaderView = UIView.init(frame: CGRect.init(x: 0, y: kScreenHeight, width: kScreenWidth, height: tempH))
        if self.titleArr.count != 0 {
            //做圆角背景
            tempHeaderView.frame = CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 56)
            tempHeaderView.backgroundColor = UIColor.white
            tempHeaderView.round(byRoundingCorners: UIRectCorner.init(arrayLiteral: .topLeft, .topRight), cornerRadi: 10)
            
            titleLab = UILabel.init(frame: CGRect.init(x: kScreenWidth/2 - 160, y: 0, width: 320, height: 56))
            titleLab.backgroundColor = UIColor.clear
            titleLab.textAlignment = .center
            titleLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
            titleLab.textColor = Theme.titleLabColor
            titleLab.numberOfLines = 0
            titleLab.text = self.titleArr[self.scrollIndex]
            tempHeaderView.addSubview(titleLab)
            
            let lineView = UIView.init(frame: CGRect.init(x: 0, y: tempHeaderView.frame.size.height - 0.5, width: kScreenWidth, height: 0.5))
            lineView.backgroundColor = UIColor.init(hex: 0xF2F7FA)
            tempHeaderView.addSubview(lineView)
        }
        
        let rightBtn = UIButton.init(frame: CGRect.init(x: tempHeaderView.frame.size.width - 134, y: 8, width: 120, height: 40))
        rightBtn.setTitle(MMLocalizedString("cancel", comment: "取消"), for: .normal)
        rightBtn.setTitle(MMLocalizedString("cancel", comment: "取消"), for: .normal)
        rightBtn.setTitleColor(Theme.mainColor, for: .normal)
        rightBtn.setTitleColor(Theme.mainColor, for: .highlighted)
        rightBtn.contentHorizontalAlignment = .right
        rightBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 16)
        rightBtn.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
        tempHeaderView.addSubview(rightBtn)
        return tempHeaderView
    }
    
    @objc func cancelAction(){
        self.hiddenActionView()
    }
    
    func setUpFooterView() -> UIView {
        let aView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: IphoneXMarginBottom))
        aView.backgroundColor = UIColor.init(hex: 0xF2F7FA)
        return aView
    }
}


extension MHScrollActionSheetView: UICollectionViewDelegate, UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.secondArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MHScrollItemCell", for: indexPath) as! MHScrollItemCell
        if self.secondArr.count > indexPath.row {
            cell.setMHScrollItemCell(data: self.secondArr[indexPath.row], aSelect: self.selectItem, hiligLight: self.defaultHighLight)
        }
        cell.delegate = self
        return cell
    }
}

extension MHScrollActionSheetView: MHScrollItemCellDelegate {
    func didSelectMHScrollItemCell(obj: Any, vIndex: NSInteger) {
        //返回数据
        self.callBack?(obj, self.scrollIndex, vIndex)
        
        self.scrollIndex = self.scrollIndex + 1
        if self.secondArr.count > self.scrollIndex {
            if self.titleArr.count > self.scrollIndex {
                self.titleLab.text = self.titleArr[self.scrollIndex]
            }
            
            if self.secondArr.count > self.scrollIndex {
                self.collectionView.scrollToItem(at: IndexPath.init(row: self.scrollIndex, section: 0), at: .centeredHorizontally, animated: true)
            }
        }else{
            //隐藏
            self.hiddenActionView()
        }
    }
}
